#!/usr/bin/env python
#
#       Results.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import wx
import wx.lib.plot as plot

class PyStats(wx.Frame):

    def __init__ (self, parent, ID, title,stats):

        wx.Frame.__init__(self, parent, ID, title, size=(300, 300))

        self.stats=stats
        font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD)
        heading = wx.StaticText(self, -1,\
            "Statistics on Module: %s"%stats[0],(25, 15))
        heading.SetFont(font)

        wx.StaticLine(self, -1, (25, 50), (250,1))

        wx.StaticText(self, -1, 'Total Cases ', (25, 80), style=wx.ALIGN_RIGHT)
        wx.StaticText(self, -1, "Passed", (25, 100), style=wx.ALIGN_RIGHT)
        wx.StaticText(self, -1, "Failures", (25, 120), style=wx.ALIGN_RIGHT)
        wx.StaticText(self, -1, "Errors", (25, 140), style=wx.ALIGN_RIGHT)

        wx.StaticText(self, -1, "%s"%self.stats[1], (250, 80))
        wx.StaticText(self, -1, "%s"\
            %(self.stats[1]-(self.stats[2][0]-self.stats[3][0])), (250,100))
        wx.StaticText(self, -1, "%s"%self.stats[2][0], (250, 120))
        wx.StaticText(self, -1, "%s"%self.stats[3][0], (250, 140))

        chart=wx.Button(self, 2, 'Chart', (175, 200), (60, 30))
        failure=wx.Button(self, 3, 'Failures', (100, 200), (60, 30))
        failure.Disable()
        error=wx.Button(self, 4, 'Errors', (25, 200), (60, 30))
        error.Disable()
        ok=wx.Button(self, 1, 'Ok', (220, 260), (60, 30))
        wx.StaticLine(self, -1, (25, 240), (250,1))

        if stats[2][0] :
            failure.Enable()
        if stats[3][0] :
            error.Enable()

        self.Bind(wx.EVT_BUTTON, self.OnOk, id=1)
        self.Bind(wx.EVT_BUTTON,self.dispChart,id=2)
        self.Bind(wx.EVT_BUTTON, self.failureDetail, id=3)
        self.Bind(wx.EVT_BUTTON, self.errorDetail, id=4)
        self.Centre()

    def OnOk(self, event):
        self.Close()

    def failureDetail(self,event):
        StatDisp(self,-1,"Failure Details",['Failure',self.stats[2][1]])
    def errorDetail(self,event):
        StatDisp(self,-1,"Error Details",['Error',self.stats[3][1]])
    def dispChart(self,event):
        StatGraph(self,-1,"Graphical View",(self.stats[1],\
            (self.stats[1]-(self.stats[2][0]-self.stats[3][0])),\
            self.stats[2][0],self.stats[3][0]))

class StatDisp(wx.Frame):

    def __init__(self, parent, id, title,data,flag=0):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,\
            wx.Size(800, 500), style=wx.MINIMIZE_BOX)
        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox = wx.BoxSizer(wx.HORIZONTAL)

        disp = wx.StaticText(panel, -1, "%s"%data[0])
        state = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE)
        ok = wx.Button(panel, 1, 'Ok')
        hbox.Add(disp, 0, wx.LEFT, 10)
        vbox.Add(hbox, 0, wx.TOP, 10)
        vbox.Add(state, 1, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT, 15)
        vbox.Add(ok, 0, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 20)
        panel.SetSizer(vbox)
        self.Centre()
        self.Bind(wx.EVT_BUTTON, self.OnOk,id=1)
        self.Show(True)

        if flag :
            for text in data[1] :
                state.AppendText(text)
        else :
            for text in data[1] :
                state.AppendText("======================================")
                state.AppendText("=====================================\n")
                state.AppendText("%s\n"%str(text[0]))
                state.AppendText("-----------------------------------------\
                    -------------------")
                state.AppendText("-----------------------------------------\
                    -------------------\n")
                state.AppendText(str(text[1]))
                state.AppendText("___________________________________________")
                state.AppendText("___________________________________________\n")

    def OnOk(self, event):
        self.Close()

class StatGraph(wx.Frame):

    def __init__(self, parent, id, title,data):
        wx.Frame.__init__(self, parent, id, title,\
            wx.DefaultPosition,wx.Size(300, 300))
        client = plot.PlotCanvas(self)
        Pass = plot.PolyLine([(1, 0), (1,data[1])],\
            legend='Pass', colour='blue', width=25)
        Fail = plot.PolyLine([(3, 0), (3,data[2])],\
            legend='Fail', colour='red', width=25)
        Error = plot.PolyLine([(5, 0), (5,data[3])],\
            legend='Error', colour='gray', width=25)
        graph = plot.PlotGraphics([Pass, Fail, Error],\
            'Module Statistics','Entity','Test Cases')
        client.Draw(graph, xAxis=(0,15), yAxis=(0,data[0]))
        self.Show(True)
